home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue46 / packages / AddInPackage / AddInU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-05-05  |  1.8 KB  |  77 lines

  1. unit AddInU;
  2.  
  3. interface
  4.  
  5. procedure BLRegister;
  6.  
  7. implementation
  8.  
  9. uses
  10.   AddInFormU, Menus, Forms;
  11.  
  12. type
  13.   //Event handlers (mostly) need to be methods of instantiated objects
  14.   TAddInHelper = class(TObject)
  15.   private
  16.     //The menu bar item, and menu item that we will add in to app
  17.     FAddInItem,
  18.     FBrowserMenuItem: TMenuItem;
  19.   public
  20.     constructor Create;
  21.     destructor Destroy; override;
  22.     //This is the event handler of our new menu item
  23.     procedure BrowserMenuItemClick(Sender: TObject);
  24.   end;
  25.  
  26. constructor TAddInHelper.Create;
  27. begin
  28.   inherited;
  29.   //Locate application's menu bar
  30.   if Assigned(Application.MainForm) and
  31.      Assigned(Application.MainForm.Menu) then
  32.   begin
  33.     FAddInItem := NewItem('&Add-In', 0, False, True, nil, 0, 'AddInItem');
  34.     FBrowserMenuItem := NewItem('&Object Browser', 0, False,
  35.       True, BrowserMenuItemClick, 0, 'BrowserMenuItem');
  36.     Application.MainForm.Menu.Items.Add(FAddInItem);
  37.     FAddInItem.Add(FBrowserMenuItem)
  38.   end
  39. end;
  40.  
  41. destructor TAddInHelper.Destroy;
  42. begin
  43.   //If package is unloaded before app is closed, tidy up
  44.   //menu items, otherwise main menu will have done it for us
  45.   if Assigned(Application.MainForm) and
  46.      Assigned(Application.MainForm.Menu) then
  47.   begin
  48.     FBrowserMenuItem.Free;
  49.     FAddInItem.Free
  50.   end;
  51.   ObjectBrowserForm.Free;
  52.   inherited
  53. end;
  54.  
  55. procedure TAddInHelper.BrowserMenuItemClick(Sender: TObject);
  56. begin
  57.   //When menu item is chosen, instantiate the browser form
  58.   if not Assigned(ObjectBrowserForm) then
  59.     ObjectBrowserForm := TObjectBrowserForm.Create(nil);
  60.   ObjectBrowserForm.Show
  61. end;
  62.  
  63. var
  64.   //Object that installs and implements menu item event handler
  65.   AddInHelper: TAddInHelper;
  66.  
  67. procedure BLRegister;
  68. begin
  69.   AddInHelper := TAddInHelper.Create;
  70. end;
  71.  
  72. initialization
  73.  
  74. finalization
  75.   AddInHelper.Free
  76. end.
  77.